package com.yanhui.utils.http;

import com.yanhui.utils.string._stringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 爬虫工具类
 */
public class _pachongUtils {


    public static List<String> getCode(String url, String title, String pat, String encoding){
        String[] titles = _stringUtils.strToArray(title, ",");
        List<String> list = new ArrayList<String>();
        String htmlSource = _cmsUtils.getHtmlResourceByUrl(url,encoding);
        htmlSource = htmlSource.replaceAll("\\s", "");
        pat = pat.replaceAll("\\s", "");
        Pattern pattern1 = Pattern.compile(pat);
        Matcher matcher1 = pattern1.matcher(htmlSource);
        while(matcher1.find()){
            String mGroupStr = "";
            for(int i = 0 ,len = titles.length; i<len; i++){
                mGroupStr += matcher1.group(i+1) + ",";
            }
            mGroupStr = mGroupStr.substring(0,mGroupStr.length()-1);
            list.add(mGroupStr);
        }
        return list;
    }


    public static List<String> getCode(String htmlSource, String title, String pat){
        String[] titles = _stringUtils.strToArray(title, ",");
        List<String> list = new ArrayList<String>();
        htmlSource = htmlSource.replaceAll("\\s", "");
        pat = pat.replaceAll("\\s", "");
        Pattern pattern1 = Pattern.compile(pat);
        Matcher matcher1 = pattern1.matcher(htmlSource);
        while(matcher1.find()){
            String mGroupStr = "";
            for(int i = 0 ,len = titles.length; i<len; i++){
                mGroupStr += matcher1.group(i+1) + ",";
            }
            mGroupStr = mGroupStr.substring(0,mGroupStr.length()-1);
            list.add(mGroupStr);
        }
        return list;
    }


    public static List<String> getCodeBySplitStr(String htmlSource, String title, String pat, String splitStr){
        String[] titles = _stringUtils.strToArray(title, ",");
        List<String> list = new ArrayList<String>();
        htmlSource = htmlSource.replaceAll("\\s", "");
        pat = pat.replaceAll("\\s", "");
        Pattern pattern1 = Pattern.compile(pat);
        Matcher matcher1 = pattern1.matcher(htmlSource);
        while(matcher1.find()){
            String mGroupStr = "";
            for(int i = 0 ,len = titles.length; i<len; i++){
                mGroupStr += matcher1.group(i+1) + splitStr;
            }
            mGroupStr = mGroupStr.substring(0,mGroupStr.length()-1);
            list.add(mGroupStr);
        }
        return list;
    }
}
